package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("setmeal")
public class SetMealController {
    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    public R<String>save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //1.接收数据封装到SetmealDto
        //2.完善套餐数据（更新人，创建人）
        Employee employee = (Employee) session.getAttribute("employee");

        setmealDto.setCreateUser(employee.getId());
        setmealDto.setUpdateUser(employee.getId());



        //3.调用业务实现添加套餐
        setmealService.save(setmealDto);
        //4.封装数据返回
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page<SetmealDto>>findAllByName(Integer page,Integer pageSize,String name){
        //1.接收三个参数（当前页,每页大小，查询条件）
        //2.调用业务层获取分页Page<SetmealDto>
        Page<SetmealDto> pageObj = setmealService.findAllByName(page, pageSize, name);
        //3封装数据返回
        return R.success(pageObj);
    }

    @DeleteMapping
    public R<String>deleteBatch(@RequestParam List<Long>ids,HttpSession session){
        //1.接收传递过来的多个id封装List<Long> ids
        //2.获取修改人
        Employee employee = (Employee) session.getAttribute("employee");

        //2.调用业务层去批量删除,传递iD
        setmealService.deleteBatch(ids,employee.getId());
        //3.封装数据返回
        return R.success("删除成功");
    }

    @RequestMapping(value = "/status/0",method = RequestMethod.POST)
    public R<String>updateStatusFByIds(@RequestParam List<Long>ids,HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Long opr = employee.getId();
        setmealService.updateStatusFById(ids,opr);
        return R.success("修改成功!!");

    }

    @RequestMapping(value = "/status/1",method = RequestMethod.POST)
    public R<String>updateStatusTByIds(@RequestParam List<Long>ids,HttpSession session){
        Employee employee = (Employee) session.getAttribute("employee");
        Long opr = employee.getId();
        setmealService.updateStatusTById(ids,opr);
        return R.success("修改成功!!");

    }

}
